from fastapi import FastAPI, Request

app = FastAPI()


@app.get("/search")
async def search(request: Request):
    #  http://127.0.0.1:8000/search?name=张三&age=20
    # 方式1：http://127.0.0.1:8000/search?name=张三&age=20
    # 接收方式1：request.query_params
    data = request.query_params
    # 方式2: http://127.0.0.1:8000/search  +json数据 参数为{"name":"zs","age":"12345"}
    # 接收方式2：request.json()
    postParms = await request.json()  # 获取请求参数，await的含义是异步获取请求参数，类型为dict，可以通过postParms["name"]获取请求参数
    print(postParms, type(postParms))
    # print("request:", request.query_params)
    username = postParms.get("name")
    userage = postParms.get("age")
    return {
        "code": 200,
        "data": {
            "username": username,
            "userage": userage
        }}


# @app.get("/search")
# # 测试⽹址 http://127.0.0.1:8000/search?name=张三&age=20
# async def search(request: Request):
#     getParms = request.query_params  # 获取请求参数，类型为dict，可以通过getParms["name"]获取请求参数
#     print(getParms)
#     return {"msg": "搜索成功"}


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host="127.0.0.1", port=8000)
